using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class JobSeekers_jobresult : System.Web.UI.Page
{
    string keyWord = "";
    string location = "";
    string category = "0";
    string industry = "0";

    string jobRange = "0";
    string itemPerPage = "15";
    string postedJob = "0";
    string sortOrder = "JobTitle";
    
    protected void Page_Load(object sender, EventArgs e)
    {
        RegisterJavaScript();
        Page searchPage = this.PreviousPage;
        
        if(searchPage != null)
        {
            TextBox textBox;
            DropDownList dropDownList;
            switch (searchPage.Title)  
            {
                case "::Search for Jobs":
                    textBox = (TextBox) 
                    searchPage.FindControl("searchKeyword");
                    keyWord = textBox.Text;
                    textBox = (TextBox) 
                    searchPage.FindControl("workLocation");
                    location = textBox.Text;
                    dropDownList = (DropDownList)searchPage.FindControl("jobCategory");
                    category = dropDownList.SelectedValue;
                    dropDownList = (DropDownList)searchPage.FindControl("jobIndustry");
                    industry = dropDownList.SelectedValue;
                    
                    dropDownList = (DropDownList)searchPage.FindControl("jobFrom");
                    jobRange = dropDownList.SelectedValue;
                    dropDownList = (DropDownList)searchPage.FindControl("showMe");
                    itemPerPage = dropDownList.SelectedValue;
                    dropDownList = (DropDownList)searchPage.FindControl("jobPosted");
                    postedJob = dropDownList.SelectedValue;
                    dropDownList = (DropDownList)searchPage.FindControl("jobSort");
                    sortOrder = dropDownList.SelectedValue;
                    break;
                case "::About RecruitVietnam.com":
                    textBox = (TextBox)
                    searchPage.FindControl("searchKeyword");
                    keyWord = textBox.Text;
                    textBox = (TextBox)
                    searchPage.FindControl("workLocation");
                    location = textBox.Text;
                    dropDownList = (DropDownList)searchPage.FindControl("jobCategory");
                    category = dropDownList.SelectedValue;
                    dropDownList = (DropDownList)searchPage.FindControl("jobIndustry");
                    industry = dropDownList.SelectedValue; 
                    
                    jobRange = "0";
                    itemPerPage = "15";
                    postedJob = "0";
                    sortOrder = "JobTitle";
                    break; 
                case "::My Account-Saved Search":                
                    GridView gridView = (GridView)
                    searchPage.FindControl("GridView1");
                    GridViewRow gridViewRow = 
                    gridView.Rows[Convert.ToInt32(
                    Request.QueryString.Get("indexRow"))];
                    
                    keyWord = gridViewRow.Cells[2].Text;
                    location = gridViewRow.Cells[3].Text;
                    category = gridViewRow.Cells[4].Text;
                    industry = gridViewRow.Cells[6].Text;
                    if(location == "&nbsp;")
                        location = "";
                    jobRange = "0";
                    itemPerPage = "15";
                    postedJob = "0";
                    sortOrder = "JobTitle";

                    break; 
                
            }
            ShowResult(keyWord, location, category, 
            industry, Convert.ToInt32(itemPerPage),
            Convert.ToInt32(jobRange),
            Convert.ToInt32(postedJob));
            
            this.searchKeyword.Text = keyWord;
            this.workLocation.Text = location;
            this.jobCategory.Value = category;
            this.jobIndustry.Value = industry;

            jobFrom.Value = jobRange;
            showMe.Value = itemPerPage;
            jobPosted.Value = postedJob;
            jobSort.Value = sortOrder;
        }
        
       
    }
    void ShowResult(string keyWord, 
    string cityName, string jobCategory, 
    string jobIndustry, int pageSize,
    int viewable, int jobNoOfDate)
    {
        try
        {
            ControlHelper  cls = new ControlHelper("");
            cls.CommandText = "SPSearchJob";
            cls.ParameterCollection = new string[6] 
            { "@keyWord", "@workLocation", 
            "@jobCategory", "@jobIndustry", 
            "@viewable", "@jobNumberOfDate" };
            cls.ValueCollection = new string[6] 
            { "%" + keyWord + "%", "%" + 
            location + "%", jobCategory, 
            jobIndustry, viewable.ToString(), 
            jobNoOfDate.ToString() };
            cls.CommandType = CommandType.StoredProcedure ;
            cls.InitProperties();
            cls.FillDataTableToGridView(
            this.GridView1, pageSize, 10, 
            PagerButtons.NumericFirstLast);
        }
        catch(Exception ex)
        {
            litError.Text = ex.Message;
        }
    }
    void RegisterJavaScript()
    {
        string javaScript = "<script language=javascript>function NewSearch(){";
        javaScript += "window.open(\"search.aspx\", target='_main');}</script>";        
        ClientScript.RegisterClientScriptBlock(this.GetType(), "JobResult", javaScript);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }

    protected void GridView1_PageIndexChanging(
    object sender, GridViewPageEventArgs e)
    {
        keyWord = searchKeyword.Text;
        location = workLocation.Text;
        category = this.jobCategory.Value;
        industry = this.jobIndustry.Value;
        
        jobRange = jobFrom.Value;
        itemPerPage = showMe.Value;
        postedJob = jobPosted.Value;
        sortOrder = jobSort.Value;
                
        
        GridView1.PageIndex = e.NewPageIndex;
        ShowResult(keyWord, location, category,
            industry, Convert.ToInt32(itemPerPage),
            Convert.ToInt32(jobRange),
            Convert.ToInt32(postedJob));
        
    }
 
    protected void btnSearch_Click(
    object sender, EventArgs e)
    {
        
        keyWord = searchKeyword.Text;
        location = workLocation.Text;
        category = this.jobCategory.Value;
        industry = this.jobIndustry.Value;
        
        jobRange = jobFrom.Value;
        itemPerPage = showMe.Value;
        postedJob = jobPosted.Value;
        sortOrder = jobSort.Value;
                 
        
        ShowResult(keyWord, location, category,
             industry, Convert.ToInt32(itemPerPage),
             Convert.ToInt32(jobRange),
             Convert.ToInt32(postedJob));
    }
    protected void btnSaveJobSearch_Click(
    object sender, EventArgs e)
    {
        if (Convert.ToString(
            Session["jsuserid"]) == "")
            Response.Redirect("login.aspx");
        keyWord = searchKeyword.Text;
        location = workLocation.Text;
        category = this.jobCategory.Value;
        industry = this.jobIndustry.Value;
        SqlServerProvider sqlServerProvider 
        = new SqlServerProvider();
        sqlServerProvider.SaveJobSearch(
        Convert.ToString(Session["jsuserid"]), 
        keyWord, location, category, industry);
    }
    
}
